
package com.compuvest.tld;

// <editor-fold defaultstate="collapsed" desc="import">
import com.compuvest.web.Item;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
// </editor-fold>

public class AdvertBlock extends TagSupport {

    private Item[] items;
    private StringBuilder block;

// <editor-fold defaultstate="collapsed" desc="doStartTag()">

    @Override
    public int doStartTag() throws JspException {

        if(items == null || items.length == 0) { return SKIP_BODY; }

        block = new StringBuilder(12000);
        try {
            String serverName = pageContext.getServletContext().getInitParameter("http");
            String imageLink = pageContext.getServletContext().getInitParameter("imgn");

            block.append("<table id='cv_adv_tb'>");
            block.append("<tr align=center>");

            for(int idx = 0; idx < items.length && idx < 25; idx++) {
                block.append("<td>");
                block.append("<div id='cv_adv' onclick=\"location.href='").append(serverName).append("Descr.jsp");
                block.append("?iid=").append(items[idx].getItemID()).append("&cid=").append(items[idx].getCategoryParent());
                block.append("&sid=").append(items[idx].getCategoryID()).append("'\">");
                block.append("<fieldset style='cursor:pointer;cursor:hand;'>");
                block.append("<div><img id='rs' onError='hideErr(this);' src='").append(imageLink);
                block.append(items[idx].getAccwareCode()).append(".jpg'/></div>");
                block.append("<div class='prc'>").append(String.format("$%,.2f", items[idx].getPrice())).append("</div>");
                block.append("<span>").append(items[idx].getItemName()).append("</span>");
                block.append("</fieldset>");
                block.append("</div>");
                block.append("</td>");
                if(idx == 4 || idx == 9 || idx == 14 || idx == 19 || idx == 24) {
                    block.append("</tr><tr align=center>");
                }
            }
            block.append("</tr>");
            block.append("</table>\n");

            pageContext.getOut().print(block.toString());
//            pageContext.getOut().print(block.toString() + "<br/>" + block.length());
        }
        catch(java.io.IOException e) {
            throw new JspTagException("AdvertBlock: " + e.getMessage());
        }

        return SKIP_BODY;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doEndTag()">

    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="release()">

    @Override
    public void release() {
        items = null;
        block = null;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="setAdvertItems()">

    public void setAdvertItems(Item[] items) {
        this.items = items;
    }
// </editor-fold>

}
